﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QD.Tools
{
    public delegate void OpenWindowSimple(object sourceData, object targetData);
    public partial class OpenWindowSimpleFrm : Form
    {
        public event OpenWindowSimple SureEvent;
        public bool IsAutoClose = true;

        private void OpenWindowSimpleFrm_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
            this.KeyDown += OpenWindowSimpleFrm_KeyDown;
        }

        void OpenWindowSimpleFrm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }

        /// <summary>
        /// 来源数据（从调用者传入）
        /// </summary>
        public object SourceData { get; set; }

        public OpenWindowSimpleFrm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 确认按钮
        /// </summary>
        public Button SureBtn
        {
            get { return this.btnSure; }
        }

        public OpenWindowSimpleFrm(string txt)
            : this()
        {
            this.textBox1.Text = txt;
        }

        public OpenWindowSimpleFrm(string txt, object sourceData)
            : this()
        {
            this.textBox1.Text = txt;
            this.SourceData = sourceData;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SureEvent(this.SourceData, this.textBox1.Text.Trim());
            if (IsAutoClose)
            {
                this.Hide();
            }
        }
    }
}